package test.day09;

import java.util.List;

/**
 * 删除有序链表中重复的部分
 */
public class RemoveDuplicatesFromSortedListII {
	public static class ListNode {
		int val;
		ListNode next;
		ListNode() {}
		ListNode(int val) { this.val = val; }
		ListNode(int val, ListNode next) { this.val = val; this.next = next; }
	}
	public ListNode deleteDuplicates(ListNode head) {
		if (head == null || head.next == null) {
			return head;
		}
		ListNode dummy = new ListNode(0);
		dummy.next = head;
		ListNode fast = dummy.next, slow = dummy;
		while (fast != null) {
			if (fast.next != null && fast.val == fast.next.val) {
				while (fast.next != null && fast.val == fast.next.val) {
					fast = fast.next;
				}
				slow.next = fast.next;
			} else {
				slow = slow.next;
			}
			fast = fast.next;
		}
		return dummy.next;
	}

	public static void main(String[] args) {
		ListNode l1 = new ListNode(1);
		ListNode l2 = new ListNode(1);
		l1.next = l2;
		new RemoveDuplicatesFromSortedListII().deleteDuplicates(l1);
	}
}
